package org.stvd.controller.guide;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.security.SecurityUserHolder;
import org.stvd.common.utils.IdWorker;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.DateUtil;
import org.stvd.entities.guide.examples.BsExampleInfo;
import org.stvd.service.guide.examples.BsExampleInfoService;

/**
 * @Title BsExampleInfoController
 * @Describtion 演示数据控制层
 * @author houzx
 * @date 2022年6月1日
 */
@Controller
@RequestMapping(value = "guide/examples")
public class BsExampleInfoController extends BaseController{
    
    @Autowired
    private BsExampleInfoService exampleInfoService;
    
    private String tmpURL = "/guide/examples/";
    
    /**
     * 管理列表
     * @param exampleInfo
     * @return
     */
    @RequestMapping(value = "manage", method = {RequestMethod.GET, RequestMethod.POST})
    public String onManage(@ModelAttribute("exampleInfo") BsExampleInfo exampleInfo) {
        QueryResult<BsExampleInfo> queryResult = exampleInfoService.queryExampleInfoResult((pageNo - 1) * pageSize, pageSize, exampleInfo.getName());
        map.put("queryResult", queryResult);
        return tmpURL + "ExampleManage.html";
    }
    
    /**
     * 新增数据
     * @param exampleInfo
     * @return
     */
    @GetMapping(value = "add")
    public String onAddLoad(@ModelAttribute("exampleInfo") BsExampleInfo exampleInfo) {
        return tmpURL + "ExampleAdd.html";
    }

    @Log(title = "新增示例数据", businessType = BusinessType.INSERT)
    @PostMapping(value = "add")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onAddAction(
        @Valid @ModelAttribute("exampleInfo") BsExampleInfo exampleInfo, BindingResult bindingResult) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        if (bindingResult.hasErrors()) {
            result.setError(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return result;
        }
        exampleInfo.setId(IdWorker.getInstance().nextId());
        exampleInfo.setCreateUser(SecurityUserHolder.getCurrentUserId());
        exampleInfo.setCreateTime(DateUtil.getSystemDate());
        exampleInfoService.insert(exampleInfo);
        
        result.setMsg("新增成功！");
        return result;
    }

    /**
     * 修改数据
     * @param id
     * @return
     */
    @GetMapping(value = "edit/{id}")
    public String onEditLoad(@PathVariable Long id) {
        map.put("exampleInfo", exampleInfoService.findByPk(BsExampleInfo.class, id));
        return tmpURL + "ExampleEdit.html";
    }

    @Log(title = "修改示例数据", businessType = BusinessType.UPDATE)
    @PostMapping(value = "edit")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onEditAction(
        @Valid @ModelAttribute("exampleInfo") BsExampleInfo exampleInfo, BindingResult bindingResult) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        if (bindingResult.hasErrors()) {
            result.setError(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return result;
        }
        exampleInfo.setModifyTime(DateUtil.getSystemDate());
        exampleInfoService.update(exampleInfo);
        map.clear();
        
        result.setMsg("修改成功！");
        return result;
    }
    
    /**
     * 删除数据
     * @param id
     * @return
     */
    @Log(title = "删除参数信息", businessType = BusinessType.DELETE)
    @PostMapping(value = "del")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onDelete(Long id, String name){
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        try {
            exampleInfoService.delete(BsExampleInfo.class, id);
            result.setMsg("‘" + name + "’删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setError("删除" + name + "失败！<br/>失败原因：" + e.getMessage());
        }
        return result;
    }

}
